<template>
  <el-dialog
    title="新增员工"
    :visible.sync="dialogShow"
    @close="closeDialog"
  >
    <el-form ref="form" :model="formData" :rules="formRules" label-width="140px">
      <el-form-item label="姓名" prop="username" size="normal">
        <el-input v-model="formData.username" style="width:320px" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile" size="normal">
        <el-input v-model="formData.mobile" style="width:320px" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry" size="normal">
        <el-date-picker v-model="formData.timeOfEntry" style="width:320px" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment" size="normal">
        <el-select v-model="formData.formOfEmployment" style="width:320px" size="normal">
          <el-option v-for="item in hireType" :key="item.id " :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber" size="normal">
        <el-input v-model="formData.workNumber" style="width:320px" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName" size="normal">
        <el-input v-model="formData.departmentName" style="width:320px" @focus="getDepList" />
        <el-tree v-if="treeShow" node-key="id" :data="deptList" style="width:320px" :props="{label:'name'}" @node-click="nodeClick" />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime" size="normal">
        <el-date-picker v-model="formData.correctionTime" style="width: 320px" />
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-button @click="dialogShow = false">取消</el-button>
        <el-button type="primary" @click="btnOk">确定</el-button>
      </el-row>
    </el-form>
  </el-dialog>

</template>+

<script>
import { tranListTreeToData } from '@/utils'
import EmployeesEnum from '@/api/constant/employees'
import { getDepartmentListApi } from '@/api/department'
import { addEmployeesApi } from '@/api/employees'
export default {
  data() {
    return {
      dialogShow: false,
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      formRules: {
        username: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
          min: 1, max: 4, message: '用户姓名为1-4位'
        }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, {
          pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }],
        correctionTime: [{ required: true, message: '入职时间', trigger: 'blur' }]
      },
      hireType: EmployeesEnum.hireType, // 聘用形式
      deptList: [], // 部门数据
      treeShow: false
    }
  },
  methods: {
  // 获取部门数据
    async getDepList() {
      const { depts } = await getDepartmentListApi()
      this.deptList = tranListTreeToData(depts, '')
      this.treeShow = true
    },
    nodeClick(val) {
      console.log(val)
      this.formData.departmentName = val.name
      this.treeShow = false
    },
    // 点击确定按钮，发起请求
    async btnOk() {
      try {
        await this.$refs.form.validate()
      } catch (error) {
        return console.log(error)
      }
      // 表单验证之后发起请求
      await addEmployeesApi(this.formData)
      // 提示成功
      this.$message.success('新增成功')
      // 列表更新
      this.$emit('updataList')
      // 弹框关闭
      this.dialogShow = false
    },
    // 弹窗关闭事件
    closeDialog() {
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      this.$refs.form.clearValidate()
    }
  }
}
</script>

<style>

</style>
